﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RatpConsole
{
    // Defines a probe with a bus, a stop, a direction, an URL
    class Probe
    {
        // Define a Probe
        private int id;
        public int Id
        {
            get { return this.id; }
            set { this.id = value; }
        }
        private String displayName;
        public String DisplayName
        {
            get { return this.displayName; }
            set { this.displayName = value; }
        }
        private String busLine;
        public String BusLine
        {
            get { return this.busLine; }
            set { this.busLine = value; }
        }
        private String busStop;
        public String BusStop
        {
            get { return this.busStop; }
            set { this.busStop = value; }
        }
        private String busDirection;
        public String BusDirection
        {
            get { return this.busDirection; }
            set { this.busDirection = value; }
        }
        private String busUrl;
        public String BusUrl
        {
            get { return this.busUrl; }
            set { this.busUrl = value; }
        }
        
        // Linked Objects
        private WebGrab webGrab;
        private WebFilter webFilter;
        // Result Fields
        private String[] resultFields;
        // Numbers Found Flags
        private Boolean numberInField2;
        public Boolean NumberInField2
        {
            get { return this.numberInField2; }
            set { this.numberInField2 = value; }
        }
        private Boolean numberInField4;
        public Boolean NumberInField4
        {
            get { return this.numberInField4; }
            set { this.numberInField4 = value; }
        }


        // Build new Probe
        public Probe(String busLine, String busStop, String busDirection, String busUrl, String displayName)
        {
            this.busLine = busLine;
            this.busStop = busStop;
            this.busDirection = busDirection;
            this.busUrl = busUrl;
            this.displayName = displayName;
            this.webGrab = new WebGrab(busUrl);
            this.webFilter = new WebFilter();
            this.resultFields = new String[4];
            this.numberInField2 = false;
            this.numberInField4 = false;
        }

        // Refresh HTML, Filter HTML, Fill resultFields
        public void refresh()
        {
            String[] results = webFilter.filter(webGrab.grab());
            resultFields[0] = results[0];
            resultFields[1] = results[1];
            resultFields[2] = results[2];
            resultFields[3] = results[3];
            this.numberInField2 = isFieldAN(results[1]);
            this.numberInField4 = isFieldAN(results[3]);
        }

        // resultFields GETTER
        public String[] getFields()
        {
            return resultFields;
        }

        private Boolean isFieldAN(String cellValue)
        {
            String nbPattern = "(\\d*) mn";
            Match matchNb = Regex.Match(cellValue, nbPattern);
            return matchNb.Success;
        }

        private int extractTime(String cellValue)
        {
            String nbPattern = "(\\d*) mn";
            Match matchNb = Regex.Match(cellValue, nbPattern);
            return int.Parse(matchNb.Groups[1].Value);
        }
    }
}
